home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
56194
/
56194.xpi
/
chrome
/
topsite.jar
/
content
/
js
/
browserOverlay.js
next >
Wrap
Text File
|
2010-01-17
|
20KB
|
499 lines
if(!topsite) var topsite={};
if(!topsite.overlay) topsite.overlay = {};
if(!topsite.httpRequestObserver) topsite.httpRequestObserver = {};
if(!topsite.webProgressListener) topsite.webProgressListener = {};
topsite.overlay = {
displayViews: null,
// Configuration parameters (with default values)
clientCacheMode: topsite.consts.CACHE_NONE,
cacheExpiredForItemInHours: 2,
maxThumbnailInScreen: 5,
maxRetriesForRelatedDownload: 5,
maxRetriesForImageDownload: 5,
intervalInSecondsForRelatedRetries: 5,
intervalInSecondsForImageRetries: 5,
lowLevelToNotifyBackInSeconds: 10,
highLevelToNotifyBackInSeconds: 40,
currTabIndex: 0,
prevTabIndex: 0,
currSelectedDocument: null,
test: function(){ alert('test'); },
initDisplayViews: function(){
topsite.overlay.displayViews = new Array(1);
topsite.overlay.displayViews[0] = new topsite.toolbarView(document.getElementById("topsiteTopicsContainer"), topsite.overlay.getTopSiteForTopic, topsite.overlay.siteClickedInView);
},
browserInit: function(){
topsite.overlay.initDisplayViews();
topsite.dal.init();
var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.addObserver(topsite.httpRequestObserver, "http-on-modify-request", false);
gBrowser.addProgressListener(topsite.webProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_ALL);
gBrowser.addEventListener("pageshow", topsite.overlay.pageShow, true);
// Register Tab events
var container = gBrowser.tabContainer;
container.addEventListener("TabOpen", topsite.overlay.browserTabAdded, false);
container.addEventListener("TabMove", topsite.overlay.browserTabMoved, false);
container.addEventListener("TabClose", topsite.overlay.browserTabRemoved, false);
container.addEventListener("TabSelect", topsite.overlay.browserTabSelected, false);
},
browserUninit: function(){
var container = gBrowser.tabContainer;
container.removeEventListener("TabOpen", topsite.overlay.browserTabAdded, false);
container.removeEventListener("TabMove", topsite.overlay.browserTabMoved, false);
container.removeEventListener("TabClose", topsite.overlay.browserTabRemoved, false);
container.removeEventListener("TabSelect", topsite.overlay.browserTabSelected, false);
gBrowser.removeProgressListener(topsite.webProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_ALL);
gBrowser.removeEventListener("pageshow", topsite.overlay.pageShow, true);
},
browserTabAdded: function(event){
},
browserTabMoved: function(event){
},
lastTabRemovedIdx: -1,
mTabIndex: 0,
browserTabRemoved: function(event){
with(topsite.overlay){
var index = gBrowser.getBrowserIndexForDocument(event.target.linkedBrowser.contentDocument);
if (index == mTabIndex){
if (index < mTabIndex)
mTabIndex = mTabIndex - 1;
currSelectedDocument = gBrowser.getBrowserAtIndex(mTabIndex).contentDocument;
lastTabRemovedIdx = index;
prevTabIndex = currTabIndex;
currTabIndex = mTabIndex;
}
}
},
browserTabSelected: function(event){
var tabIndex = gBrowser.getBrowserIndexForDocument(event.target.linkedBrowser.contentDocument);
setTimeout(function(){
with(topsite.overlay){
if (parseInt(lastTabRemovedIdx) >= 0 && parseInt(lastTabRemovedIdx) < parseInt(tabIndex)){
tabIndex = tabIndex - 1;
}
lastTabRemovedIdx = -1;
mTabIndex = tabIndex;
currSelectedDocument = gBrowser.getBrowserAtIndex(mTabIndex).contentDocument;
prevTabIndex = currTabIndex;
currTabIndex = mTabIndex;
clearPrevResultsInViews();
var tabObject = gBrowser.getBrowserAtIndex(tabIndex);
var currTopics = tabObject.getAttribute("Topics");
if (currTopics != ""){
var arrTopicsForUrl = currTopics.split(",");
topsite.overlay.populateTopicsInViews(arrTopicsForUrl);
}
}
}, 0);
},
pageShowDoc: null,
pageShow: function(event){
var doc = event.originalTarget;
if (doc instanceof HTMLDocument){
if (doc.defaultView.frameElement){
while (doc.defaultView.frameElement){
doc=doc.defaultView.frameElement.ownerDocument;
}
}
var tabIndex = gBrowser.getBrowserIndexForDocument(doc);
if (parseInt(tabIndex) < 0)
return;
var docMainWin = gBrowser.getBrowserAtIndex(tabIndex).contentDocument;
if (docMainWin != doc)
return;
topsite.overlay.pageShowDoc = doc;
setTimeout(function(){
if (doc.location){
topsite.overlay.pageShowCheckRedirect(doc.location.href);
}
}, 1000);
}
},
pageShowCheckRedirect: function(newLoc){
var TsIsRedirect = false;
var TsRedirectFrom = null;
if (topsite.overlay.pageShowDoc.location != null){
if (topsite.overlay.pageShowDoc.location.href != newLoc){
TsIsRedirect = true;
TsRedirectFrom = newLoc;
}
topsite.overlay.processNewURL(topsite.overlay.pageShowDoc, topsite.overlay.pageShowDoc.location.href, TsIsRedirect, TsRedirectFrom, true);
}
},
processNewURL: function(p_doc, p_newUrl, p_bTsIsRedirect, p_redirectedFrom, p_TsIsPageLoaded, referrer) {
if (p_doc instanceof HTMLDocument) {
var doc = p_doc;
if (doc.defaultView && doc.defaultView.frameElement) {
while (doc.defaultView.frameElement) {
doc=doc.defaultView.frameElement.ownerDocument;
p_newUrl = doc.location;
}
}
var tabIndex = gBrowser.getBrowserIndexForDocument(doc);
if (parseInt(tabIndex) < 0){
return;
}
try{
if (p_newUrl.substring(0, 4) != "http"){
return;
}
}catch(e3){}
var docMainWin = gBrowser.getBrowserAtIndex(tabIndex).contentDocument;
if (docMainWin != doc){
return;
}
var tabObject = gBrowser.getBrowserAtIndex(tabIndex);
var newUrl = p_newUrl;
var currUrl = tabObject.getAttribute("TsCurrentUrl");
var blnNewTab = false;
if (currUrl == ""){
var prevTabObject = null;
if (topsite.overlay.currTabIndex == tabIndex){
prevTabObject = gBrowser.getBrowserAtIndex(topsite.overlay.prevTabIndex);
}
else{
prevTabObject = gBrowser.getBrowserAtIndex(topsite.overlay.currTabIndex);
}
if (prevTabObject != null){
tabObject.setAttribute("TsCurrentUrl", prevTabObject.getAttribute("TsCurrentUrl"));
tabObject.setAttribute("TsOldUrl", prevTabObject.getAttribute("TsOldUrl"));
currUrl = tabObject.getAttribute("TsCurrentUrl");
blnNewTab = true;
}
}
var blnTsIsLink = false;
if (referrer != null){
if (referrer.spec == currUrl){
blnTsIsLink = true;
}
else if (referrer.spec.indexOf(encodeURIComponent(newUrl)) > 0){
blnTsIsLink = true;
}
else if (referrer.spec.indexOf(encodeURIComponent(newUrl).replace(/~/g, "%257E")) > 0){
blnTsIsLink = true;
}
else if (p_bTsIsRedirect && referrer.spec.indexOf(encodeURIComponent(p_redirectedFrom)) > 0){
blnTsIsLink = true;
}
else if (p_bTsIsRedirect && referrer.spec.indexOf(encodeURIComponent(p_redirectedFrom).replace(/~/g, "%257E")) > 0){
blnTsIsLink = true;
}
}
if (currUrl != null){
if (!blnNewTab && currUrl == newUrl){
if (p_TsIsPageLoaded){
tabObject.setAttribute("TsIsPageLoaded", true);
//topsite.overlay.siteLoadedInBrowser(tabIndex);
}
return;
}
}
if (p_TsIsPageLoaded){
tabObject.setAttribute("TsIsPageLoaded", true);
}
else{
tabObject.setAttribute("TsIsPageLoaded", false);
}
var TsNavType = tabObject.getAttribute("TsNavType");
var prevTsNavType = tabObject.getAttribute("PrevTsNavType");
if (TsNavType == null)
TsNavType = topsite.consts.NAV_TYPE_REGULAR;
if (prevTsNavType == null)
prevTsNavType = topsite.consts.NAV_TYPE_REGULAR;
tabObject.setAttribute("TsNavType", TsNavType);
tabObject.setAttribute("TsIsRedirect", p_bTsIsRedirect);
if (p_bTsIsRedirect){
tabObject.setAttribute("TsRedirectFrom", p_redirectedFrom);
}
if (!p_bTsIsRedirect){
tabObject.setAttribute("TsOldUrl", tabObject.getAttribute("TsCurrentUrl"));
}
else {
if (tabObject.getAttribute("TsIsLink") == "true"){
blnTsIsLink = true;
}
}
tabObject.setAttribute("TsCurrentUrl", newUrl);
tabObject.setAttribute("TsIsLink", blnTsIsLink);
tabObject.setAttribute("TsgotResponse", false);
tabObject.setAttribute("PrevTsNavType", TsNavType);
tabObject.setAttribute("TsNavType", topsite.consts.NAV_TYPE_REGULAR);
var TsOldUrl = tabObject.getAttribute("TsOldUrl");
topsite.overlay.getSiteTopics(newUrl, TsOldUrl, TsNavType, tabIndex, false, blnTsIsLink);
}
},
clearPrevResults: function(tabIndex){
if (tabIndex == gBrowser.tabContainer.selectedIndex){
topsite.overlay.clearPrevResultsInViews();
}
var tabObject = gBrowser.getBrowserAtIndex(tabIndex);
tabObject.setAttribute("Topics", "");
},
clearPrevResultsInViews: function(){
for (var i = 0; i < topsite.overlay.displayViews.length; ++i){
var displayView = topsite.overlay.displayViews[i];
displayView.clear();
}
},
getSiteTopics: function(newUrl, prevUrl, TsNavType, tabIndex, force, isFromLink){
topsite.overlay.clearPrevResults(tabIndex);
var cancelAdv = false;
if (TsNavType == topsite.consts.NAV_TYPE_ADV)
cancelAdv = true;
var arrExtraData = new Array(2);
arrExtraData[0] = newUrl;
arrExtraData[1] = tabIndex;
topsite.dal.getTopicsForUrl(newUrl, prevUrl, TsNavType, isFromLink, cancelAdv, topsite.overlay.setSiteTopics, arrExtraData);
},
setSiteTopics: function(arrTopicsForUrl, arrExtraData){
var newUrl = arrExtraData[0];
var tabIndex = arrExtraData[1];
var tabObject = gBrowser.getBrowserAtIndex(tabIndex);
if (tabObject.getAttribute("TsCurrentUrl") == newUrl){
tabObject.setAttribute("Topics", arrTopicsForUrl.join(","));
tabObject.setAttribute("TsgotResponse", true);
if (tabIndex == gBrowser.tabContainer.selectedIndex){
topsite.overlay.populateTopicsInViews(arrTopicsForUrl);
}
}
},
populateTopicsInViews: function(arrTopicsForUrl){
for (var i = 0; i < topsite.overlay.displayViews.length; ++i){
var displayView = topsite.overlay.displayViews[i];
displayView.populate(arrTopicsForUrl);
}
},
getTopSiteForTopic: function(strTopic, objState){
var tabIndex = gBrowser.tabContainer.selectedIndex;
var tabObject = gBrowser.getBrowserAtIndex(tabIndex);
var strCurrUrl = tabObject.getAttribute("TsCurrentUrl");
var arrExtraData = new Array(3);
arrExtraData[0] = strCurrUrl;
arrExtraData[1] = tabIndex;
arrExtraData[2] = objState;
topsite.dal.getTopSiteForTopic(strTopic, strCurrUrl, topsite.overlay.setTopSiteForTopic, arrExtraData);
},
setTopSiteForTopic: function(arrTopSiteUrls, arrExtraData){
var currUrl = arrExtraData[0];
var tabIndex = arrExtraData[1];
var objState = arrExtraData[2];
var tabObject = gBrowser.getBrowserAtIndex(tabIndex);
if (tabObject.getAttribute("TsCurrentUrl") == currUrl){
if (tabIndex == gBrowser.tabContainer.selectedIndex){
for (var i = 0; i < topsite.overlay.displayViews.length; ++i){
var displayView = topsite.overlay.displayViews[i];
displayView.showTopSite(arrTopSiteUrls, objState);
}
}
}
},
siteClickedInView: function(strUrl, siteTopic, isAdv){
var tabIndex = gBrowser.tabContainer.selectedIndex;
var tabObject = gBrowser.getBrowserAtIndex(tabIndex);
if (isAdv){
tabObject.setAttribute("TsNavType", topsite.consts.NAV_TYPE_ADV);
}else{
tabObject.setAttribute("TsNavType", topsite.consts.NAV_TYPE_CLICKED);
}
topsite.overlay.openUrlInBrowser(strUrl, siteTopic);
},
findTopSite: function(){
var term = encodeURIComponent(document.getElementById('topsiteSearchBox').value);
topsite.overlay.findTopSiteForTerm(term);
},
findTopSiteForTerm: function(term){
var searchUrl = "http://www.topsite.com/search?sterm=" + term + "&stype=5";
topsite.overlay.openUrlInBrowser(searchUrl);
},
openUrlInBrowser: function(strUrl, siteTopic){
var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
var uriRef = ios.newURI("http://www.topsite.com/", null, null);
if (siteTopic)
uriRef = ios.newURI("http://www.topsite.com/best/" + siteTopic, null, null);
var objNewTab = gBrowser.addTab();
gBrowser.getBrowserForTab(objNewTab).loadURI(encodeURI(strUrl), uriRef);
gBrowser.selectedTab = objNewTab;
}
};
topsite.httpRequestObserver = {
getDocfromDOM: function(aChannel, aSubject) {
try {
if(aChannel.notificationCallbacks)
var notificationCallbacks = aChannel.notificationCallbacks;
else
var notificationCallbacks = aSubject.loadGroup.notificationCallbacks;
var callback = notificationCallbacks.getInterface(Components.interfaces.nsIDOMWindow);
try {
if(callback.document != null)
var doc = callback.document;
} catch (e) {
return null;
}
} catch (e) {
return null;
}
return doc;
},
observe : function(aSubject, aTopic, aData) {
try
{
if (aSubject != null){
var aChannel = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
var request = aChannel.QueryInterface(Components.interfaces.nsIRequest);
if (request.loadFlags & 2686976){
var doc = topsite.httpRequestObserver.getDocfromDOM(aChannel, aSubject);
if (doc != null){
if (request.loadFlags & 262144){
setTimeout(function(){
topsite.overlay.processNewURL(doc, aChannel.URI.spec, true, aChannel.originalURI.spec, false, aChannel.referrer);
},0);
} else {
setTimeout(function(){
topsite.overlay.processNewURL(doc, aChannel.URI.spec, false, null, false, aChannel.referrer);
},0);
}
}
}
}
} catch (e) {}
}
};
topsite.webProgressListener = {
SW_STATE_START: 0x00000001,
SW_STATE_REDIRECTING: 0x00000002,
SW_STATE_TRANSFERRING: 0x00000004,
SW_STATE_STOP: 0x00000010,
SW_STATE_IS_REQUEST: 0x00010000,
SW_STATE_IS_DOCUMENT: 0x00020000,
SW_STATE_IS_NETWORK: 0x00040000,
SW_STATE_IS_WINDOW: 0x00080000,
QueryInterface: function(aIID) {
if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
aIID.equals(Components.interfaces.nsISupports))
return topsite.overlay;
throw Components.results.NS_NOINTERFACE;
},
onStateChange: function(aWebProgress, aRequest, aFlag, aStatus) {
try {
if (aFlag & topsite.webProgressListener.SW_STATE_IS_WINDOW) {
if((aFlag & topsite.webProgressListener.SW_STATE_START) && (aFlag & topsite.webProgressListener.SW_STATE_IS_DOCUMENT)) {
topsite.overlay.processNewURL(aWebProgress.DOMWindow.document, aRequest.name, false, null, false);
}
else if (aFlag & topsite.webProgressListener.SW_STATE_STOP){
var aChannel = aRequest.QueryInterface(Components.interfaces.nsIChannel);
if (!(aChannel.originalURI.equals(aChannel.URI))) {
topsite.overlay.processNewURL(aWebProgress.DOMWindow.document, aChannel.URI.spec, true, aChannel.originalURI.spec, false);
}
}
}
} catch (e) {}
return 0;
},
onLocationChange: function(aProgress, aRequest, aURI) { return 0; },
onProgressChange: function( webProgress , request , curSelfProgress , maxSelfProgress , curTotalProgress , maxTotalProgress ) { return 0; },
onStatusChange: function() {return 0;},
onSecurityChange: function() {return 0;},
onLinkIconAvailable: function() {return 0;}
};
window.addEventListener("load", function() {topsite.overlay.browserInit();}, false);
window.addEventListener("unload", function() {topsite.overlay.browserUninit();}, false);